package com.livestocksolutions.ui;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

/* -- Version History --
 *  1.0 : 03 August 2012 - Initial version.
 *  
 *  1.1 : 08 September 2012 - Made elements that belong to each view take on a unique id
 *                         to prevent any clashing of ids which has a bad effect on the
 *                         data that is shown after a screen rotation and such.
 */

/**
 * A class to create the layout for the list of Contacts
 * @author Adam Parr
 * @version 1.1
 * 
 * @.modified 09 September 2012
 */
public class ListContactArrayAdapter extends BaseArrayAdapter {
	/**
	 * Size of the allowable array passed
	 */
	public static final int SIZE = 4;
	/**
	 * Type of the Owner position within the String[position][{@value #TYPE}] array passed in the Constructor.
	 */
	public static final int TYPE				= 0;
	/**
	 * For a type Farm or Business Owner String[position][{@value #STREETADDRESS}] will be the Street address value,
	 * otherwise this will be an empty string
	 */
	public static final int STREETADDRESS 	= 1;
	/**
	 * For a type Farm or Business Owner String[position][{@value #TOWNADDRESS}] will be the Town value,
	 * otherwise this will be an empty string
	 */
	public static final int TOWNADDRESS 		= 2;
	/**
	 * For a type Farm or Business Owner String[position][{@value #POSTCODEADDRESS}] will be the Postcode value,
	 * otherwise this will be an empty string
	 */
	public static final int POSTCODEADDRESS 	= 3;
	
	
	public static final int TYPE_ADDRESS 		= 0;
	public static final int TYPE_EMAIL 		= 1;
	public static final int TYPE_PHONE 		= 2;
	public static final int TYPE_FAX 			= 3;
	public static final int TYPE_OTHER 		= 4;
	
	/**
	 * An array containing a list of possible contact types, this is initialised on the first
	 * creation of this Object type
	 */
	private static String[] array;
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Contacts to represent in the ListView.
	 */
	public ListContactArrayAdapter(BaseActivity context, String[][] values) {
		super(context, values);
		if(array==null){
			array = context.getResources().getStringArray(R.array.contact_type);
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		setView(R.layout.item_contact);
		View rowView = super.getView(position, convertView, parent);
		
		final Spinner contactType = (Spinner) rowView.findViewById(R.id.contactType);
		final TextView contactField = (TextView) rowView.findViewById(R.id.contactField);
		final TextView addressTown = (TextView) rowView.findViewById(R.id.addressTown);
		final TextView addressPostcode = (TextView) rowView.findViewById(R.id.addressPostal);
		
		// Create this array to be the full length that will be needed to avoid ArrayIndexOutOfBoundsException
		if(values[position].length<SIZE){
			String[] orig = values[position];
			values[position] = new String[SIZE];
			int x = 0;
			for(String s : orig){
				values[position][x]=s;
				x++;
			}
		}
		
		contactType.setId((1+TYPE)*1000+position);
		
		contactField.setId((1+STREETADDRESS)*1000+position);
		contactField.setText(values[position][STREETADDRESS]);
		
		addressTown.setId((1+TOWNADDRESS)*1000+position);
		addressTown.setText(values[position][TOWNADDRESS]);
		
		addressPostcode.setId((1+POSTCODEADDRESS)*1000+position);
		addressPostcode.setText(values[position][POSTCODEADDRESS]);
		
		contactType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int index, long id) {
				addressTown.setVisibility(View.GONE);
				addressPostcode.setVisibility(View.GONE);
				values[position][TYPE]=String.valueOf(index);
				
				// Set what is visible and not visible
				switch(index){
					case TYPE_ADDRESS:
						contactField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
						contactField.setHint(context.getResources().getString(R.string.contact_address_street));
						addressTown.setVisibility(View.VISIBLE);
						addressPostcode.setVisibility(View.VISIBLE);
						break;
					case TYPE_EMAIL:
						contactField.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS);
						contactField.setHint(context.getResources().getString(R.string.contact_email));
						break;
					case TYPE_PHONE:
					case TYPE_FAX:
						contactField.setInputType(InputType.TYPE_CLASS_PHONE);
						contactField.setHint(context.getResources().getString(R.string.contact_phone));
						
						break;
					case TYPE_OTHER:
						contactField.setInputType(InputType.TYPE_CLASS_TEXT);
						contactField.setHint(context.getResources().getString(R.string.contact_other));
						break;
					default:
				}
			}

			public void onNothingSelected(AdapterView<?> parent) {}
		});
		
		contactField.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				// TODO possible validation - dependent on contactType.getSelectedItem() value
				values[position][STREETADDRESS]=s.toString();
			}
		});
		
		addressTown.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				// TODO possible validation
				values[position][TOWNADDRESS]=s.toString();
			}
		});
		
		addressPostcode.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				// TODO validation
				values[position][POSTCODEADDRESS]=s.toString();
			}
		});
		
		// Determine the contact type this contact is
		contactType.setSelection(Integer.parseInt(values[position][TYPE]!=null?values[position][TYPE]:"0"));
	
		return rowView;
	}
}
